<?php
namespace addon\cms\validate;
use app\common\validate\ValidateBase;
class Posts extends ValidateBase
{
    // 验证规则
    protected $rule = [
        'title'                 => 'require',
        'cateid'                => 'gt:0',
        'content'               => 'require',
        'to_cateid'             => 'require|gt:0',
        'action'                => 'require',
        'url'                   => 'require|check_json',
        'token'                 => 'require',
        'posts_images_upload_limit'=>'gt:0',
        'posts_video_upload_limit'=>'gt:0',
        'html_save_path'=>'require',
        'cut_posts_length'=>'require|gt:0',
        'keyword_replace_num'=>'egt:0',
        'exptable'              =>'require',
        'column'                =>'require',
        'rpstring'              =>'require',
        'tostring'              =>'require'
    ];

    // 验证提示
    protected $message = [
        'title.require'         => '文章名称不能为空',
        'cateid'                => '请选择文章分类',
        'content'               => '内容不能为空',
        'to_cateid'             => '请选择目标分类',
        'action'                => '请选择要操作的选项',
        'url'                   => '链接地址错误',
        'token'                 => '密钥必须',
        'exptable'              => '请指定表名',
        'column'                => '要替换的字段必须',
        'rpstring'                => '被替换内容必须',
        'tostring'                => '替换内容必须',
        'html_save_path.require'=>'文档HTML默认保存路径必须',
        'keyword_replace_num.egt'=>'替换次数必须为大于等于0'
    ];

    // 应用场景
    protected $scene = [
        'add'  =>  ['title','cateid','content'],
        'add_posts_images'=>['title','cateid'],
        'add_posts_videos'=>['title','cateid'],
        'move' =>  ['cateid','to_cateid'],
        'wrong' => ['action'],
        'baidu' => ['url','token'],
        'posts_setting' => ['posts_images_upload_limit','posts_video_upload_limit','html_save_path','cut_posts_length','keyword_replace_num'],
        'replace'=>['exptable','column','rpstring','tostring']
    ];

    protected function check_json($value){
        $data = json_decode($value);
        if ($data && is_object($data)){
            return true;
        }else{
            return "链接不是合格的json串";
        }
    }
}